<template>
    <div class="mysearch">
        <div class="search">
            <div class="search_left"><van-icon name="arrow-left" @click="back" /></div>
            <div class="search_right">
                <div class="search-box">
                    <span class="sym"><van-icon name="search" @click="check" /></span>
                    <input type="text" @keydown.enter="check" v-model.trim="val" placeholder="请输入商品名" />
                </div>
            </div>
        </div>
        <div class="content">
            <div class="item" v-for="i in arr.res" @click="on(i.index_product_id)">
                <div class="img">
                    <img :src="i.product_img" alt="" />
                </div>
                <div class="con">
                    <div class="cont">
                        <div class="title">
                            {{ i.product_name }}
                        </div>
                    </div>
                    <div class="conb">
                        <span class="price-sym">¥</span><span class="price">{{ i.product_price }}</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { useRouter } from "vue-router";
import { getSearch } from "../../request/index.js";
let router = useRouter();
let val = ref("");
let arr = reactive({ res: [] });
let back = () => {
    router.go(-1);
};
let check = () => {
    if (val.value.length == 0) {
        alert("内容不能为空");
        return;
    }
    getSearch({ val: val.value }).then((res) => {
        arr.res = res.data;
        alert(res.msg);
    });
};
let on = (x) => {
    router.push({ path: "/commoditydetails/" + x, query: { id: x } });
};
</script>

<style lang="scss" scoped>
.mysearch {
    width: 100%;
    height: 100%;
    .search {
        display: flex;
        height: 3.125rem;
        align-items: center;
        padding: 0 20px;
        box-sizing: border-box;
    }
    .search_left {
        width: 3.125rem;
    }
    .search_right {
        flex: 1;
        .search-box {
            background-color: #f6f6f6;
            padding: 0.4375rem 0.625rem;
            border-radius: 0.75rem;
            display: flex;
            align-items: center;
        }
        span,
        input {
            background-color: transparent;
        }
        input {
            flex: 1;
            text-align: center;
        }
        &::input-placeholder {
            color: white;
        }
    }
    .content {
        margin-top: 1.25rem;
        padding: 0 0.625rem;
        box-sizing: border-box;
        margin-bottom: 1.25rem;
        .item {
            margin-top: 0.625rem;
            height: 100px;
            width: 100%;
            background-color: #f5f5f5;
            display: flex;
            border-radius: 0.5rem;
            overflow: hidden;
        }
        .img {
            width: 6.25rem;
            img {
                width: 100%;
                height: 100%;
            }
        }
        .con {
            flex: 1;
            display: flex;
            flex-direction: column;
            .cont {
                padding: 0.3125rem 0.5rem;
                box-sizing: border-box;
                flex: 1;
                font-size: 15px;
            }
            .title {
                line-height: 1.25rem;
                overflow: hidden;
                text-overflow: ellipsis;
                display: -webkit-box;
                -webkit-box-orient: vertical;
                -webkit-line-clamp: 2;
                color: #848484;
            }
            .conb {
                padding: 0.3125rem 0.5rem;
                box-sizing: border-box;
                flex: 1;
                display: flex;
                align-items: center;
                color: #ff5154;
                .price-sym {
                    font-size: 0.875rem;
                    box-sizing: border-box;
                    padding-top: 0.25rem;
                }
                .price {
                    font-size: 1.25rem;
                }
            }
        }
    }
}
</style>
